package com.inei.cie.capituloi;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloIDao;
import com.inei.cie.dao.CaratulaDao;
import com.inei.cie.dao.PrincipalDao;
import com.inei.cie.entidades.Dist;
import com.inei.cie.entidades.P1_C;
import com.inei.cie.entidades.P1_C_20N;
import com.inei.cie.entidades.PCar;
import com.inei.cie.entidades.Prov;
import com.inei.cie.utilitarios.Variables;

public class CapituloI_SeccionC_Anexo_Activity extends Activity {

	public Variables var;
	public String id_local;
	public CapituloIDao capitulo;
	public int Nro_Pred;
	public int Nro_IE;
	public int NroCMod, NroA;
	public String CodMod;
	public int Nro;
	public Bundle b;

	public int d = 0;
	public TextView tvLocal;
	public P1_C anexo;
	public List<P1_C_20N> lista;
	public List<Prov> lProv;
	public List<Dist> lDist;
	public PCar pcar;
	public CaratulaDao caratula;
	public PrincipalDao principal;

	public TextView tvSeccionC;
	public EditText et1C1, et1C4, et1C5, et1C61, et1C62, et1C63, et1C64,
			et1C65, et1C66, et1C67, et1C68, et1C69;
	public EditText et1C7, et1C8, et1C9, et1C11, et1C12, et1C13, et1C14,
			et1C15, et1C16entero, et1C16decimal;
	public EditText et1C17entero, et1C17decimal, et1C19;
	public Spinner sp1C2, sp1C3, sp1C6, sp1C8, sp1C9, sp1C10, sp1C12, sp1C14,
			sp1C18, spUnidad1, spUnidad2;
	public LinearLayout ll1C20;
	public List<EditText> lista20;

	public TableRow tr1C20, tr1C19, tr1C11, tr1C12, tr1C13, tr1C14, tr1C15,
			tr1C16;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_i_seccion_c_anexo);
		IniciarComponentes();
	}

	public void IniciarComponentes() {
		capitulo = new CapituloIDao(this);
		var = new Variables();
		b = getIntent().getExtras();
		id_local = b.getString(var.ID_LOCAL);
		Nro_Pred = b.getInt(var.NRO_PRED);
		CodMod = b.getString(var.NRO_CODMOD);
		Nro_IE = b.getInt(var.NRO_IE);
		Nro = b.getInt("nro");
		NroA = b.getInt("NroA");
		NroCMod = b.getInt("NroCMod");

		caratula = new CaratulaDao(this);
		pcar = caratula.ObtenerCaratula(id_local, Nro_Pred);
		principal = new PrincipalDao(this);
		Prov prov = new Prov();
		prov.setCod_dep(0);
		prov.setCod_prov(0);
		prov.setNombre("SELECCIONAR PROVINCIA");
		lProv = principal.ListadoProvinciaPorId(pcar.getPC_A_1_Dep());
		lProv.add(0, prov);
		tvSeccionC = (TextView) findViewById(R.id.tvSeccionC);
		String[] texto = tvSeccionC.getText().toString().split("-");
		tvSeccionC.setText(texto[0].trim() + " " + Nro + " -  "
				+ texto[1].trim() + " " + CodMod);

		tr1C11 = (TableRow) findViewById(R.id.tr1C11);
		tr1C12 = (TableRow) findViewById(R.id.tr1C12);
		tr1C13 = (TableRow) findViewById(R.id.tr1C13);
		tr1C14 = (TableRow) findViewById(R.id.tr1C14);
		tr1C15 = (TableRow) findViewById(R.id.tr1C15);
		tr1C16 = (TableRow) findViewById(R.id.tr1C16);
		tr1C19 = (TableRow) findViewById(R.id.tr1C19);
		tr1C20 = (TableRow) findViewById(R.id.tr1C20);

		et1C1 = (EditText) findViewById(R.id.et1C1);

		spUnidad1 = (Spinner) findViewById(R.id.spUnidad1);
		spUnidad2 = (Spinner) findViewById(R.id.spUnidad2);

		sp1C2 = (Spinner) findViewById(R.id.sp1C2);

		sp1C3 = (Spinner) findViewById(R.id.sp1C3);
		sp1C2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View view, int pos,
					long arg3) {
				Dist dist = new Dist();
				dist.setCod_dep(0);
				dist.setCod_prov(0);
				dist.setCod_dist(0);
				dist.setNombre("SELECCIONAR DISTRITO");
				if (pos != 0) {
					lDist = principal.ListadoDistritoPorId(lProv.get(pos)
							.getCod_dep(), lProv.get(pos).getCod_prov());
					lDist.add(0, dist);
					ArrayAdapter<Dist> adap = new ArrayAdapter<Dist>(
							CapituloI_SeccionC_Anexo_Activity.this,
							android.R.layout.simple_list_item_1, lDist);
					sp1C3.setAdapter(adap);
					if (anexo != null && d == 0) {
						for (int i = 0; i < lDist.size(); i++) {
							if (lDist.get(i).getCod_dist() == anexo
									.getP1_C_3_DistCod()) {
								sp1C3.setSelection(i);
								break;
							}
						}
						d = 1;
					}
				} else {
					lDist = new ArrayList<Dist>();
					lDist.add(dist);
					ArrayAdapter<Dist> adap = new ArrayAdapter<Dist>(
							CapituloI_SeccionC_Anexo_Activity.this,
							android.R.layout.simple_list_item_1, lDist);
					sp1C3.setAdapter(adap);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		ArrayAdapter<Prov> adaptador = new ArrayAdapter<Prov>(this,
				android.R.layout.simple_list_item_1, lProv);
		sp1C2.setAdapter(adaptador);

		et1C4 = (EditText) findViewById(R.id.et1C4);
		et1C5 = (EditText) findViewById(R.id.et1C5);
		sp1C6 = (Spinner) findViewById(R.id.sp1C6);
		et1C61 = (EditText) findViewById(R.id.et1C61);
		et1C62 = (EditText) findViewById(R.id.et1C62);
		et1C63 = (EditText) findViewById(R.id.et1C63);
		et1C64 = (EditText) findViewById(R.id.et1C64);
		et1C65 = (EditText) findViewById(R.id.et1C65);
		et1C66 = (EditText) findViewById(R.id.et1C66);
		et1C67 = (EditText) findViewById(R.id.et1C67);
		et1C68 = (EditText) findViewById(R.id.et1C68);
		et1C69 = (EditText) findViewById(R.id.et1C69);
		et1C7 = (EditText) findViewById(R.id.et1C7);
		sp1C8 = (Spinner) findViewById(R.id.sp1C8);
		sp1C8.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View v, int pos,
					long arg3) {
				if (pos == 0) {
					et1C8.setVisibility(View.VISIBLE);
					et1C8.requestFocus();
				} else {
					et1C8.setVisibility(View.GONE);
					et1C8.setText("");
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1C8 = (EditText) findViewById(R.id.et1C8);
		sp1C9 = (Spinner) findViewById(R.id.sp1C9);
		sp1C9.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View v, int pos,
					long arg3) {
				if (pos == 4 || pos == 5) {
					et1C9.setVisibility(View.VISIBLE);
					et1C9.setEnabled(true);
					et1C9.requestFocus();
				} else {
					et1C9.setText("");
					et1C9.setVisibility(View.GONE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1C9 = (EditText) findViewById(R.id.et1C9);
		et1C9.setEnabled(false);
		sp1C10 = (Spinner) findViewById(R.id.sp1C10);
		sp1C10.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				if (pos == 5) {
					tr1C11.setVisibility(View.GONE);
					et1C11.setText("");
				} else {
					tr1C11.setVisibility(View.VISIBLE);
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1C11 = (EditText) findViewById(R.id.et1C11);
		et1C11.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence texto, int arg1, int arg2,
					int arg3) {
				if (texto.toString().trim().length() != 0) {
					tr1C12.setVisibility(View.GONE);
					sp1C12.setSelection(0);
					et1C12.setText("");
					tr1C13.setVisibility(View.GONE);
					et1C13.setText("");
					tr1C14.setVisibility(View.GONE);
					sp1C14.setSelection(0);
					et1C14.setText("");
					tr1C15.setVisibility(View.GONE);
					et1C15.setText("");
				} else {
					tr1C12.setVisibility(View.VISIBLE);
					tr1C13.setVisibility(View.VISIBLE);
					tr1C14.setVisibility(View.VISIBLE);
					tr1C15.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
		sp1C12 = (Spinner) findViewById(R.id.sp1C12);
		sp1C12.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View v, int pos,
					long arg3) {
				if (pos == 10) {
					tr1C13.setVisibility(View.GONE);
					et1C13.setText("");
					et1C12.setVisibility(View.GONE);
					et1C12.setText("");
				} else {
					tr1C13.setVisibility(View.VISIBLE);
					if (pos == 9) {
						et1C12.setVisibility(View.VISIBLE);
						et1C12.requestFocus();
					} else {
						et1C12.setText("");
						et1C12.setVisibility(View.GONE);
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1C12 = (EditText) findViewById(R.id.et1C12);
		et1C13 = (EditText) findViewById(R.id.et1C13);
		et1C13.setFocusable(false);
		et1C13.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText et = (EditText) v;
				AbrirCalendar(et.getText().toString(), et);

			}
		});
		sp1C14 = (Spinner) findViewById(R.id.sp1C14);
		sp1C14.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View v, int pos,
					long arg3) {
				if (pos == 10) {
					tr1C15.setVisibility(View.GONE);
					et1C15.setText("");
					et1C14.setVisibility(View.GONE);
					et1C14.setText("");
				} else {
					tr1C15.setVisibility(View.VISIBLE);
					if (pos == 9) {
						et1C14.setVisibility(View.VISIBLE);
						et1C14.requestFocus();
					} else {
						et1C14.setVisibility(View.GONE);
						et1C14.setText("");
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1C14 = (EditText) findViewById(R.id.et1C14);
		et1C15 = (EditText) findViewById(R.id.et1C15);
		et1C15.setFocusable(false);
		et1C15.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText et = (EditText) v;
				AbrirCalendar(et.getText().toString(), et);

			}
		});
		et1C16entero = (EditText) findViewById(R.id.et1C16entero);
		et1C16decimal = (EditText) findViewById(R.id.et1C16decimal);
		et1C17entero = (EditText) findViewById(R.id.et1C17entero);
		et1C17decimal = (EditText) findViewById(R.id.et1C17decimal);
		sp1C18 = (Spinner) findViewById(R.id.sp1C18);
		sp1C18.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				if (pos == 2 || pos == 0) {
					et1C19.setText("");
					et1C19.setEnabled(false);
				} else {
					et1C19.requestFocus();
					et1C19.setEnabled(true);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		et1C19 = (EditText) findViewById(R.id.et1C19);
		et1C19.setEnabled(false);
		et1C19.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (!s.toString().equals("")) {
					if (Integer.parseInt(s.toString()) > 10) {
						et1C19.setText("");
						Toast.makeText(
								CapituloI_SeccionC_Anexo_Activity.this,
								"El n\u00famero de locales escolares, otras instituciones o serviciones con las que se comparte el predio no debe ser mayor a 10",
								Toast.LENGTH_LONG).show();
						et1C19.requestFocus();
					} else {
						if (s.toString().equals("") || s.toString().equals("0")) {

							ll1C20.removeAllViewsInLayout();
							lista20.clear();
						} else {
							ll1C20.removeAllViewsInLayout();
							lista20 = new ArrayList<EditText>();
							int cant = Integer.parseInt(s.toString());
							for (int i = 0; i < cant; i++) {
								Agregar20("");
							}
						}
					}
				}

				if (s.toString().equals("") || s.toString().equals("0")) {
					ll1C20.removeAllViewsInLayout();
					lista20.clear();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
		ll1C20 = (LinearLayout) findViewById(R.id.ll1C20);
		lista20 = new ArrayList<EditText>();
		CargarData();
		et1C1.requestFocus();
	}

	public void CargarData() {
		anexo = new P1_C();
		anexo.setId_local(id_local);
		anexo.setP1_A_2_NroIE(Nro_IE);
		anexo.setP1_A_2_9A_CMod(CodMod);
		anexo.setP1_A_2_9_AnexNro(Nro);
		anexo.setP1_A_2_9_Nro(NroA);
		anexo.setP1_A_2_9_NroCMod(NroCMod);
		anexo = capitulo.ObtenerAnexoC(anexo);
		if (anexo.getP1_C_1_CodLoc_Anex() != null) {
			et1C1.setText(anexo.getP1_C_1_CodLoc_Anex());
		}
		for (int i = 0; i < lProv.size(); i++) {
			if (anexo.getP1_C_2_ProvCod() == lProv.get(i).getCod_prov()) {
				sp1C2.setSelection(i);
				break;
			}
		}
		sp1C18.setSelection(anexo.getP1_C_18_Comp());
		if (anexo.getSwCompleto() == 1) {
			et1C19.setText(anexo.getP1_C_19_CompCan() + "");

		} else {
			et1C19.setText(anexo.getP1_C_19_CompCan() == 0 ? "" : anexo
					.getP1_C_19_CompCan() + "");
		}
		if (anexo.getP1_C_4_CCPP() != null) {
			et1C4.setText(anexo.getP1_C_4_CCPP().toUpperCase());
		}
		if (anexo.getP1_C_5_NucleoUrb() != null) {
			et1C5.setText(anexo.getP1_C_5_NucleoUrb().toUpperCase());
		}
		if (anexo.getP1_C_6Dir_1_Tvia() != 0) {
			sp1C6.setSelection(anexo.getP1_C_6Dir_1_Tvia());

		}
		if (anexo.getP1_C_6Dir_2_Nomb() != null) {
			et1C61.setText(anexo.getP1_C_6Dir_2_Nomb().toUpperCase());
		}
		if (anexo.getP1_C_6Dir_Nro() != null) {
			et1C62.setText(anexo.getP1_C_6Dir_Nro().toUpperCase());
		}
		if (anexo.getP1_C_6Dir_4_Piso() != null) {
			et1C63.setText(anexo.getP1_C_6Dir_4_Piso().toUpperCase());
		}
		if (anexo.getP1_C_6Dir_5_Mz() != null) {
			et1C64.setText(anexo.getP1_C_6Dir_5_Mz().toUpperCase());
		}
		if (anexo.getP1_C_6Dir_6_Lt() != null) {
			et1C65.setText(anexo.getP1_C_6Dir_6_Lt().toUpperCase());
		}
		if (anexo.getP1_C_6Dir_7_Sect() != null) {
			et1C66.setText(anexo.getP1_C_6Dir_7_Sect().toUpperCase());
		}
		if (anexo.getP1_C_6Dir_8_Zona() != null) {
			et1C67.setText(anexo.getP1_C_6Dir_8_Zona().toUpperCase());
		}
		if (anexo.getP1_C_6Dir_9_Et() != null) {
			et1C68.setText(anexo.getP1_C_6Dir_9_Et().toUpperCase());
		}
		if (anexo.getP1_C_6Dir_10_Km() != null) {
			et1C69.setText(anexo.getP1_C_6Dir_10_Km().toUpperCase());
		}
		if (anexo.getP1_C_7_RefDir() != null) {
			et1C7.setText(anexo.getP1_C_7_RefDir().toUpperCase());
		}
		sp1C8.setSelection(anexo.getP1_C_8_InmTip());

		if (anexo.getP1_C_8_InmCod() != null) {
			et1C8.setText(anexo.getP1_C_8_InmCod().toUpperCase());
		}
		sp1C9.setSelection(anexo.getP1_C_9_Prop());
		if (anexo.getP1_C_9_Prop() != 0) {
			et1C9.setText(anexo.getP1_C_9_Prop_O());
		}

		sp1C10.setSelection(anexo.getP1_C_10_AntReg_Cod());
		if (anexo.getP1_C_11_AntReg_Nro() != null) {
			et1C11.setText(anexo.getP1_C_11_AntReg_Nro().toUpperCase());
		}
		sp1C12.setSelection(anexo.getP1_C_12_Tipo_TProp());

		if (anexo.getP1_C_12_Tipo_TProp_O() != null) {
			et1C12.setText(anexo.getP1_C_12_Tipo_TProp_O().toUpperCase());
		}
		et1C13.setText(anexo.getP1_C_13_FecTit());
		sp1C14.setSelection(anexo.getP1_C_14_DocPos());
		if (anexo.getP1_C_14_DocPos_O() != null) {
			et1C14.setText(anexo.getP1_C_14_DocPos_O().toUpperCase());
		}
		if (anexo.getP1_C_15_DocPos_Fech() != null) {
			et1C15.setText(anexo.getP1_C_15_DocPos_Fech().toUpperCase());
		}
		if (anexo.getP1_C_16_At_Pred() != 0) {
			String a1 = anexo.getP1_C_16_At_Pred() + "";
			String[] area1 = a1.split("\\.");
			et1C16entero.setText(area1[0]);
			spUnidad1.setSelection(anexo.getP1_C_16_Tip_At_Pred());
		}
		if (anexo.getP1_C_17_At_Local() != 0) {
			String a2 = anexo.getP1_C_17_At_Local() + "";
			String[] area2 = a2.split("\\.");
			et1C17entero.setText(area2[0]);
			spUnidad2.setSelection(anexo.getP1_C_17_Tip_At_Local());
		}

		lista = capitulo.ObtenerAnexoC20(anexo);
		if (lista != null && lista20 != null) {
			for (int i = 0; i < lista.size(); i++) {
				if (i < lista20.size()) {
					if (lista20.get(i) != null) {
						if (lista.get(i).getP1_C_20_NombComp() != null) {
							lista20.get(i).setText(
									lista.get(i).getP1_C_20_NombComp()
											.toUpperCase());
						}
					}
				}
			}
		}
	}

	public void Agregar20(String nomb) {
		EditText et = new EditText(this);
		et.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
		et.setText(nomb);
		anexo.setP1_C_6Dir_8_Zona(et1C67.getText().toString().toUpperCase());
		ll1C20.addView(et);
		lista20.add(et);
	}

	@Override
	public void onBackPressed() {
		Intent irLista = new Intent(this,
				CapituloI_SeccionC_Lista_Anexos_Activity.class);
		irLista.putExtras(b);
		startActivity(irLista);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_guardar_calc, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir ?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnGuardar:
			GuardarData();
			break;
		case R.id.mnCalculadora:
			Intent i = new Intent();
			i.setClassName("com.android.calculator2",
					"com.android.calculator2.Calculator");
			startActivity(i);
			break;
		case R.id.mnFoto:
			Intent foto = new Intent(CapituloI_SeccionC_Anexo_Activity.this,
					CapituloIX_Activity.class);
			foto.putExtras(getIntent().getExtras());
			foto.putExtra("pregunta", true);
			startActivity(foto);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void GuardarData() {
		anexo.setP1_C_1_CodLoc_Anex(et1C1.getText().toString());
		anexo.setP1_C_2_ProvCod(sp1C2.getSelectedItemPosition());
		anexo.setP1_C_3_DistCod(sp1C3.getSelectedItemPosition());
		anexo.setP1_C_4_CCPP(et1C4.getText().toString().toUpperCase());
		anexo.setP1_C_5_NucleoUrb(et1C5.getText().toString().toUpperCase());
		anexo.setP1_C_6Dir_1_Tvia(sp1C6.getSelectedItemPosition());
		anexo.setP1_C_6Dir_2_Nomb(et1C61.getText().toString().toUpperCase());
		anexo.setP1_C_6Dir_Nro(et1C62.getText().toString().toUpperCase());
		anexo.setP1_C_6Dir_4_Piso(et1C63.getText().toString().toUpperCase());
		anexo.setP1_C_6Dir_5_Mz(et1C64.getText().toString().toUpperCase());
		anexo.setP1_C_6Dir_6_Lt(et1C65.getText().toString().toUpperCase());
		anexo.setP1_C_6Dir_7_Sect(et1C66.getText().toString().toUpperCase());
		anexo.setP1_C_6Dir_8_Zona(et1C67.getText().toString().toUpperCase());
		anexo.setP1_C_6Dir_9_Et(et1C68.getText().toString().toUpperCase());
		anexo.setP1_C_6Dir_10_Km(et1C69.getText().toString().toUpperCase());
		anexo.setP1_C_7_RefDir(et1C7.getText().toString().toUpperCase());
		anexo.setP1_C_8_InmTip(sp1C8.getSelectedItemPosition());
		anexo.setP1_C_8_InmCod(et1C8.getText().toString().toUpperCase());
		anexo.setP1_C_9_Prop(sp1C9.getSelectedItemPosition());
		anexo.setP1_C_9_Prop_O(et1C9.getText().toString().toUpperCase());

		anexo.setP1_C_10_AntReg_Cod(sp1C10.getSelectedItemPosition());
		anexo.setP1_C_11_AntReg_Nro(et1C11.getText().toString().toUpperCase());
		anexo.setP1_C_12_Tipo_TProp(sp1C12.getSelectedItemPosition());
		anexo.setP1_C_12_Tipo_TProp_O(et1C12.getText().toString().toUpperCase());
		anexo.setP1_C_13_FecTit(et1C13.getText().toString());
		anexo.setP1_C_14_DocPos(sp1C14.getSelectedItemPosition());
		anexo.setP1_C_14_DocPos_O(et1C14.getText().toString().toUpperCase());
		anexo.setP1_C_15_DocPos_Fech(et1C15.getText().toString().toUpperCase());

		float pre16 = 0;

		if (!et1C16entero.getText().toString().equals("")) {
			String num = (et1C16entero.getText().toString().equals("") ? 0
					: et1C16entero.getText().toString())
					+ "."
					+ (et1C16decimal.getText().toString().equals("") ? 0
							: et1C16decimal.getText().toString());
			pre16 = Float.parseFloat(num);
			anexo.setP1_C_16_Tip_At_Pred(spUnidad1.getSelectedItemPosition());
			anexo.setP1_C_16_At_Pred(pre16);
		}

		float pre17 = 0;

		if (!et1C17entero.getText().toString().equals("")) {
			String num = (et1C17entero.getText().toString().equals("") ? 0
					: et1C17entero.getText().toString())
					+ "."
					+ (et1C17decimal.getText().toString().equals("") ? 0
							: et1C16decimal.getText().toString());
			pre17 = Float.parseFloat(num);
			anexo.setP1_C_17_Tip_At_Local(spUnidad2.getSelectedItemPosition());
			anexo.setP1_C_17_At_Local(pre17);
		}

		anexo.setP1_C_18_Comp(sp1C18.getSelectedItemPosition());
		if (!et1C19.getText().toString().equals("")) {
			anexo.setP1_C_19_CompCan(Integer.parseInt(et1C19.getText()
					.toString()));
		}

		if (!et1C19.getText().toString().equals("")
				&& !et1C19.getText().toString().equals("0")) {
			lista = new ArrayList<P1_C_20N>();
			for (int i = 0; i < lista20.size(); i++) {
				P1_C_20N obj = new P1_C_20N();
				obj.setId_local(anexo.getId_local());
				obj.setP1_A_2_NroIE(anexo.getP1_A_2_NroIE());
				obj.setP1_A_2_9_NroCMod(anexo.getP1_A_2_9_NroCMod());
				obj.setP1_A_2_9_Nro(anexo.getP1_A_2_9_Nro());
				obj.setP1_A_2_9A_CMod(anexo.getP1_A_2_9A_CMod());
				obj.setP1_A_2_9_AnexNro(anexo.getP1_A_2_9_AnexNro());
				obj.setP1_C_20_Nro(i + 1);
				obj.setP1_C_20_NombComp(lista20.get(i).getText().toString()
						.toUpperCase());
				obj.setSwCompleto(1);
				lista.add(obj);
			}
		} else {
			lista = null;
		}
		anexo.setSwCompleto(1);

		if (et1C1.getText().toString().length() == 0) {
			et1C1.requestFocus();
			Toast.makeText(this,
					"Debe ingresar el C\u00f3digo del Local Escolar del Anexo",
					Toast.LENGTH_SHORT).show();
		} else if (et1C1.getText().toString().length() != 6) {
			et1C1.requestFocus();
			Toast.makeText(
					this,
					"El C\u00f3digo del Local Escolar del Anexo debe tener 6 d\u00edgitos",
					Toast.LENGTH_SHORT).show();
		} else if (sp1C2.getSelectedItemPosition() == 0) {
			sp1C2.requestFocus();
			Toast.makeText(this, "Debe seleccionar la provincia",
					Toast.LENGTH_SHORT).show();
		} else if (sp1C3.getSelectedItemPosition() == 0) {
			sp1C3.requestFocus();
			Toast.makeText(this, "Debe seleccionar el distrito",
					Toast.LENGTH_SHORT).show();
		} else if (sp1C8.getSelectedItemPosition() == 0
				&& et1C8.getText().toString().trim().length() < 7) {
			et1C8.requestFocus();
			Toast.makeText(
					this,
					"El C\u00f3digo del Inmueble debe tener un m\u00ednimo de 7 d\u00edgitos",
					Toast.LENGTH_SHORT).show();
		}
		if (sp1C6.getSelectedItemPosition() == 0) {
			Toast.makeText(this, "Debe seleccionar el tipo de v\u00eda",
					Toast.LENGTH_SHORT).show();
		} else if (et1C61.getText().toString().trim().length() == 0) {
			Toast.makeText(this, "Debe ingresar el nombre de v\u00eda",
					Toast.LENGTH_SHORT).show();
		} else if ((et1C64.getText().toString().trim().length() == 0 && et1C65
				.getText().toString().trim().length() == 0)
				&& et1C62.getText().toString().trim().length() == 0) {
			Toast.makeText(this, "Debe ingresar el nro de puerta",
					Toast.LENGTH_SHORT).show();
		} else if (et1C63.getText().toString().trim().length() == 0) {
			Toast.makeText(this, "Debe ingresar el piso", Toast.LENGTH_SHORT)
					.show();
		} else if (et1C64.getText().toString().trim().length() != 0
				&& et1C65.getText().toString().trim().length() == 0) {
			Toast.makeText(this, "Debe ingresar el lote", Toast.LENGTH_SHORT)
					.show();
		} else if (et1C64.getText().toString().trim().length() == 0
				&& et1C65.getText().toString().trim().length() != 0) {
			Toast.makeText(this, "Debe ingresar la manzana", Toast.LENGTH_SHORT)
					.show();
		} else if (et1C16entero.getText().toString().equals("")) {
			Toast.makeText(this,
					"Ingrese el \u00e1rea del terreno que ocupa el predio",
					Toast.LENGTH_SHORT).show();
			et1C16entero.requestFocus();
		} else if (et1C17entero.getText().toString().equals("")) {
			Toast.makeText(
					this,
					"Ingrese el \u00e1rea del terreno que ocupa el anexo del local escolar en este predio",
					Toast.LENGTH_SHORT).show();
			et1C17entero.requestFocus();
		} /*else if (Integer.parseInt(et1C16entero.getText().toString()) < Integer
				.parseInt(et1C17entero.getText().toString())) {
			Toast.makeText(
					this,
					"El \u00e1rea del terreno que ocupa el predio debe ser mayor o igual al \u00e1rea del terreno que ocupa el anexo del local escolar en este predio",
					Toast.LENGTH_LONG).show();
			et1C17entero.requestFocus();
		} */else if (sp1C18.getSelectedItemPosition() == 1
				&& (et1C19.getText().toString().trim().equals("") || Integer
						.parseInt(et1C19.getText().toString().trim()) == 0)) {
			Toast.makeText(
					this,
					"Porfavor ingrese una cantidad mayor a 0 en la pregunta 19",
					Toast.LENGTH_LONG).show();
			et1C19.requestFocus();
		} else if (Validar20()) {
			Toast.makeText(
					this,
					"Debe completar los " + lista.size()
							+ " nombres solicitados en la pregunta 20",
					Toast.LENGTH_LONG).show();
		} else {

			if (capitulo.GuardarAnexoC(anexo, lista)) {
				Toast.makeText(this, "Se guardo con \u00e9xito",
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(this, "No se pudo guardar", Toast.LENGTH_SHORT)
						.show();
			}
		}
	}

	public boolean Validar20() {
		if (lista == null) {
			return false;
		}
		for (int i = 0; i < lista.size(); i++) {
			if (lista.get(i).getP1_C_20_NombComp().trim().length() == 0) {
				lista20.get(i).requestFocus();
				return true;
			}
		}
		return false;
	}

	public void AbrirCalendar(String f, final EditText et) {

		final Calendar c = Calendar.getInstance();
		int dia = c.get(Calendar.DAY_OF_MONTH);
		int mes = c.get(Calendar.MONTH);
		int anio = c.get(Calendar.YEAR);
		if (f.length() != 0) {
			String[] valores = f.split("/");
			dia = Integer.parseInt(valores[0]);
			mes = Integer.parseInt(valores[1]) - 1;
			anio = Integer.parseInt(valores[2]);
		}

		DatePickerDialog fecha = new DatePickerDialog(this,
				new OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						c.set(Calendar.YEAR, year);
						c.set(Calendar.MONTH, monthOfYear);
						c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
						SimpleDateFormat sdf = new SimpleDateFormat(
								"dd/MM/yyyy", Locale.US);
						if (!CompararFechasMenos(
								sdf.format(Calendar.getInstance().getTime()),
								sdf.format(c.getTime()))) {
							Toast.makeText(
									CapituloI_SeccionC_Anexo_Activity.this,
									"La fecha ingresada no puede ser mayor a la fecha actual",
									Toast.LENGTH_SHORT).show();
						} else {
							et.setText(sdf.format(c.getTime()));
						}
					}
				}, anio, mes, dia);
		fecha.setButton(DatePickerDialog.BUTTON_NEUTRAL, "Borrar",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						et.setText("");
					}
				});

		fecha.show();
	}

	public boolean CompararFechasMenos(String f1, String f2) {
		try {
			String[] fecha1 = f1.split("/");
			String[] fecha2 = f2.split("/");
			if (fecha1[2].compareTo(fecha2[2]) < 0) {
				return false;
			} else if (fecha1[2].compareTo(fecha2[2]) == 0) {
				if (fecha1[1].compareTo(fecha2[1]) < 0) {
					return false;
				} else if (fecha1[1].compareTo(fecha2[1]) == 0) {
					if (fecha1[0].compareTo(fecha2[0]) < 0) {
						return false;
					}
				}
			}
			return true;
		} catch (Exception e) {
			Toast.makeText(this, "Error al comparar fechas", Toast.LENGTH_SHORT)
					.show();
			return false;
		}
	}
}
